﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClientBackBone.EDDN
{
    public class Ship
    {
        public string ShipName;
        public string ShipType;
        public string Hull;
        public string PowerGen;
        public string Thrusters;
        public string FrameShift;
        public string LifeSupport;
        public string PowerDistributer;
        public string Sensors;
        public string FuelTank;
        public List<string> HardPoints = new List<string>();
        public List<string> Utilities = new List<string>();
        public List<string> Internals = new List<string>();
        internal static JObject lookUP;
        public int ShipIndex;
        public int Cost;

        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(this.LookUpValue(this.ShipType));
            stringBuilder.Append("/");
            stringBuilder.Append(this.LookUpValue(this.Hull));
            stringBuilder.Append(this.LookUpValue(this.PowerGen));
            stringBuilder.Append(this.LookUpValue(this.Thrusters));
            stringBuilder.Append(this.LookUpValue(this.FrameShift));
            stringBuilder.Append(this.LookUpValue(this.LifeSupport));
            stringBuilder.Append(this.LookUpValue(this.PowerDistributer));
            stringBuilder.Append(this.LookUpValue(this.Sensors));
            stringBuilder.Append(this.LookUpValue(this.FuelTank));
            foreach (string item in this.HardPoints)
            {
                stringBuilder.Append(this.LookUpValue(item));
            }

            foreach (string item in this.Utilities)
            {
                stringBuilder.Append(this.LookUpValue(item));
            }

            foreach (string item in this.Internals)
            {
                stringBuilder.Append(this.LookUpValue(item));
            }

            stringBuilder.Append("?bn=");
            stringBuilder.Append(this.ShipName);

            return stringBuilder.ToString();
        }

        private string LookUpValue(string p)
        {
            if (Ship.lookUP["Mapping"][p] == null)
            {
                return "-";
            }

            return Ship.lookUP["Mapping"][p].Value<string>();
        }

        public override bool Equals(object obj)
        {
            bool match = false;
            if (obj is Ship)
            {
                match = string.Compare(obj.ToString(), this.ToString(), false, CultureInfo.InvariantCulture) == 0;
            }

            return match;
        }

        public override int GetHashCode()
        {
            return this.ToString().GetHashCode();
        }
    }
}
